home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_08_10
/
8n10034a
< prev
next >
Wrap
Text File
|
1990-06-08
|
47KB
|
1,710 lines
Listing 3: transact.c
/*************************
File: TRANSACT.C
Created
By: Russell Cook
*************************/
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "environ.h"
#include "transact.h"
/*===== MODULE STATIC VARIABLE TYPES =====*/
/* offset: long file offset where user believes file is positioned */
/* fd: valid file descriptor for CLOSED_FILE (when free) */
/* openMode: mode with which file was opened */
/* openPerms: access permissions used for open */
/* index: handle of associated trans. file (bFileType == USER_FILE) */
/* nUseCount: # of associated user files (file type is trans. file) */
/* bFileType: USER_FILE or a valid transaction log file type specifier */
/* npcFileName: near pointer to a null-terminated filename */
typedef struct {
long offset;
int fd;
int openMode;
int openPerms;
union {
FHANDLE index;
int nUseCount;
} unionData;
BOOL bFileType;
char NEAR *npcFileName;
} TFile;
#define FILEOFFSET( npFile ) (npFile)->offset
#define FILEDESCRIPTOR( npFile ) (npFile)->fd
#define FILEMODE( npFile ) (npFile)->openMode
#define FILEPERMS( npFile ) (npFile)->openPerms
#define FILEINDEX( npFile ) (npFile)->unionData.index
#define FILEUSECOUNT( npFile ) (npFile)->unionData.nUseCount
#define FILETYPE( npFile ) (npFile)->bFileType
#define FILENAME( npFile ) (npFile)->npcFileName
/* lOffset: offset in USER data file where data should be written */
/* DataBytes: number of bytes of data extracted from USER file */
/* FileMode: read/write accessibility used to open file */
/* FilePermissions: access permissions used when USER file opened */
/* NOTE: NEW_FILE already stripped */
/* sOperation: action which caused transaction logging */
/* NameLen: # of bytes (including null) in filename field */
typedef struct {
long lOffset;
unsigned int DataBytes;
int FileMode;
int FilePermissions;
int NameLen;
short sOperation;
} TRollBackStruct;
#define ROLLOFFSET( npRoll ) (npRoll)->lOffset
#define ROLLBYTES( npRoll ) (npRoll)->DataBytes
#define ROLLMODE( npRoll ) (npRoll)->FileMode
#define ROLLPERMS( npRoll ) (npRoll)->FilePermissions
#define ROLLOP( npRoll ) (npRoll)->sOperation
#define ROLLNAMELEN( npRoll ) (npRoll)->NameLen
/*===== MODULE STATIC MANIFEST CONSTANTS =====*/
#ifdef MSC51_ENV
# include <io.h>
# include <stdlib.h>
# include <malloc.h>
# include <dos.h>
# include <sys/locking.h>
# include <limits.h>
# define ERROR_CODE 1
# define SUCCESS_CODE 0
# define NEW_FILE (O_CREAT | O_EXCL)
# define READ_FILE O_RDONLY
# define WRITE_FILE O_WRONLY
# define BINARY_FILE O_BINARY
# define SHARED_FILE S_IWRITE | S_IREAD
# define SYNC_FILE 0
# define _LPN_MAX_ _MAX_DIR
# define _LFN_MAX_ ( _MAX_FNAME + _MAX_EXT )
# define MAXUINT UINT_MAX
# define OPEN(name,mode,perms,pFd) \
((*pFd = open(name,mode,perms)) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define READ(fd,buf,count,pNum) _dos_read(fd,buf,count,pNum)
# define WRITE(fd,buf,count,pNum) _dos_write(fd,buf,count,pNum)
# define LSEEK(fd,offset,whence,pPos) \
((*pPos = lseek(fd,offset,whence)) == -1L ? ERROR_CODE : SUCCESS_CODE)
# define CLOSE(fd) (close(fd) == -1 ? ERROR_CODE : SUCCESS_CODE)
# define CHSIZE(fd,lbytes) \
(chsize(fd,lbytes) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define UNLINK(npName) \
(unlink(npName) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define FSTAT(fd,buf) \
(fstat(fd,buf) == -1 ? ERROR_CODE : SUCCESS_CODE )
# ifdef MIXED_MODEL
# define FSTRCPY(lpDest,lpSource) farStrcpy(lpDest,lpSource)
# define FSTRLEN(lpString) farStrlen(lpString)
# else
# define FSTRCPY(lpDest,lpSource) strcpy(lpDest,lpSource)
# define FSTRLEN(lpString) strlen(lpString)
# endif /* MIXED_MODEL */
# define STRCMP(npStr1,npStr2) stricmp(npStr1,npStr2)
# define STRDUP( npString ) strdup( npString )
# define MALLOC( bytes ) malloc( bytes )
# define REALLOC( ptr, bytes ) realloc( ptr, bytes )
# define FREE( ptr ) free( ptr )
# define BYTE_BITS 8
# define BITS(x) (BYTE_BITS * sizeof(x))
#endif /* MS-DOS environment */
#ifdef TURBOC_ENV
# include <io.h>
# include <stdlib.h>
# include <alloc.h>
# include <dos.h>
# include <limits.h>
# define ERROR_CODE 1
# define SUCCESS_CODE 0
# define NEW_FILE (O_CREAT | O_EXCL)
# define READ_FILE O_RDONLY
# define WRITE_FILE O_WRONLY
# define BINARY_FILE O_BINARY
# define SHARED_FILE S_IWRITE | S_IREAD
# define SYNC_FILE 0
# define _LPN_MAX_ 128
# define _LFN_MAX_ ( 13 )
# define MAXUINT UINT_MAX
# define OPEN(name,mode,perms,pFd) \
((*pFd = open(name,mode,perms)) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define READ(fd,buf,count,pNum) \
((*pNum = read(fd,buf,count)) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define WRITE(fd,buf,count,pNum) \
((*pNum = write(fd,buf,count)) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define LSEEK(fd,offset,whence,pPos) \
((*pPos = lseek(fd,offset,whence)) == -1L ? ERROR_CODE : SUCCESS_CODE)
# define CLOSE(fd) (close(fd) == -1 ? ERROR_CODE : SUCCESS_CODE)
# define CHSIZE(fd,lbytes) \
(chsize(fd,lbytes) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define UNLINK(npName) \
(unlink(npName) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define FSTAT(fd,buf) \
(fstat(fd,buf) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define FSTRCPY(lpDest,lpSource) strcpy(lpDest,lpSource)
# define FSTRLEN(lpString) strlen(lpString)
# define STRCMP(npStr1,npStr2) stricmp(npStr1,npStr2)
# define STRDUP( npString ) strdup( npString )
# define MALLOC( bytes ) malloc( bytes )
# define REALLOC( ptr, bytes ) realloc( ptr, bytes )
# define FREE( ptr ) free( ptr )
# define BYTE_BITS 8
# define BITS(x) (BYTE_BITS * sizeof(x))
#endif /* TurboC environment */
#ifdef SCOUNIX_ENV
# include <stdlib.h>
# include <values.h>
# include <mnttab.h>
# include <limits.h>
# define ERROR_CODE 1
# define SUCCESS_CODE 0
# define NEW_FILE (O_CREAT | O_EXCL)
# define READ_FILE O_RDONLY
# define WRITE_FILE O_WRONLY
# define BINARY_FILE 0
# define SYNC_FILE O_SYNC
# define SHARED_FILE 0666
# define _LPN_MAX_ LPNMAX
# define _LFN_MAX_ LFNMAX
# define MAXUINT UINT_MAX
extern int open(char *,int,int);
extern int close( int );
extern long lseek( int, long, int );
extern int read( int, char *, unsigned );
extern int write( int, char *, unsigned );
extern int fstat(int,struct stat *);
extern int unlink( char * );
# define OPEN(name,mode,perms,pFd) \
((*pFd = open(name,mode,perms)) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define READ(fd,buf,count,pNum) \
((*pNum = read(fd,buf,count)) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define WRITE(fd,buf,count,pNum) \
((*pNum = write(fd,buf,count)) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define LSEEK(fd,offset,whence,pPos) \
((*pPos = lseek(fd,offset,whence)) == -1L ? ERROR_CODE : SUCCESS_CODE)
# define CLOSE(fd) (close(fd) == -1 ? ERROR_CODE : SUCCESS_CODE)
# define CHSIZE(fd,lbytes) ERROR_CODE
# define UNLINK(npName) \
(unlink(npName) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define FSTAT(fd,buf) \
(fstat(fd,buf) == -1 ? ERROR_CODE : SUCCESS_CODE )
# define FSTRCPY(lpDest,lpSource) strcpy(lpDest,lpSource)
# define FSTRLEN(lpString) strlen(lpString)
# define STRCMP(npStr1,npStr2) strcmp(npStr1,npStr2)
# define STRDUP( npString ) strdup( npString )
# define MALLOC( bytes ) malloc( bytes )
# define REALLOC( ptr, bytes ) realloc( ptr, bytes )
# define FREE( ptr ) free( ptr )
#endif /* some variation of Unix environment */
#ifndef READ
1 = 0; /* didn't get an environment specifier */
#endif
#define HANDLE_MASK ((FHANDLE)(1 << (BITS(FHANDLE) - 2) ))
#define USER_FILE ((BOOL)0)
#define CLOSED_FILE ((int)-1)
#define INCR_COUNT ( 5 )
#define BAD_FTYPE ((BOOL)(1 << (BITS( BOOL ) - 1)))
#define APPEND_OP ((short) 1)
#define OVERWR_OP ((short) 2)
#define CHSIZE_OP ((short) 4)
#define DATA_AREA 256
#define XFER_SIZE (sizeof(TRollBackStruct)